//
//  UIFont+SJAdd.swift
//  SJSwiftKit
//
//  Created by swordjoy on 2017/3/7.
//  Copyright © 2017年 swordjoy. All rights reserved.
//

import Foundation
import UIKit

public extension UIFont {
  
  /// 计算字符串在特地地方高度
  ///
  /// - Parameters:
  ///   - width: 字符串显示的宽度
  ///   - font: 字体
  ///   - text: 显示的字符串
  /// - Returns: 字符串在特定宽度下的高度
  public static func calculateHeight(width: CGFloat, font: UIFont, text: String) -> CGFloat {
    var size: CGSize = CGSize.zero
    if text.sj.length > 0 {
      let frame: CGRect = text.boundingRect(with: CGSize(width: width, height: 999999),
                                            options: [.usesLineFragmentOrigin, .usesFontLeading],
                                            attributes: [NSFontAttributeName: font], context: nil)
      size = CGSize(width: frame.size.width, height: frame.size.height + 1)
    }
    return size.height
  }
  
  public static func calculateWidth(font: UIFont, text: String) -> CGFloat {
    var size: CGSize = CGSize.zero
    if text.sj.length > 0 {
      let frame: CGRect = text.boundingRect(with: CGSize(width: 999999, height: 999999), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
      size = CGSize(width: frame.size.width, height: frame.size.height + 1)
    }
    return size.width
  }
    
}
